function nthPrime(n) {
    let primes = [2, 3, 5, 7];

    while (primes.length < n) {
        let last = primes[primes.length - 1];

        for (let i = last + 2; i <= last * last && primes.length < n; i += 2) {
            let flag = true;
            for (let j = 0; j < primes.length; j++) {
                if (i % primes[j] === 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) primes.push(i);
        }

        console.log("primes=",primes);
    }
    return primes[n - 1];
}

console.log(nthPrime(50));